package parsing

import (
	"data-process/function"
	"strconv"
	"strings"
)

// IntegerParse 整型解析器
// age|1-10: 1
type IntegerParse struct {
	field    string            // 属性
	key      string            // key
	value    int               // 默认的value值
	size     int               // 需要生成的数据量
	function function.FuncBase // 函数
}

func NewIntegerParse(key string, value int, size int) *IntegerParse {
	if size == 0 {
		size = 10
	}
	return &IntegerParse{
		key:   key,
		value: value,
		size:  size,
	}
}

func (n *IntegerParse) parse() {
	if strings.Contains(n.key, "|") {
		keys := strings.Split(n.key, "|")
		n.field = keys[0]
		if strings.Contains(keys[1], "-") {
			split := strings.Split(keys[1], "-")
			min, _ := strconv.Atoi(split[0])
			max, _ := strconv.Atoi(split[1])
			n.function = function.RangeNumber(n.size, min, max)
		}
	} else {
		n.field = n.key
		n.function = function.DefaultNumber(n.size, n.value)
	}
}

func (n *IntegerParse) Build() (string, []interface{}, error) {
	n.parse()
	return n.field, n.function.Build(), nil
}
